using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using TVDesigner.GUI.Elements;

using Screen = TVDesigner.Entities.Elements.Screen;

namespace TVDesigner.GUI.Controls
{
    public partial class DiagramTabControl : UserControl
    {
        #region Atributos
        private List<DiagramPanel> _Diagrams;
        private FMain _MainForm; 
        #endregion

        #region Propriedades
        public FMain MainForm
        {
            get { return _MainForm; }
            set { _MainForm = value; }
        }
        #endregion

        public DiagramTabControl()
        {
            InitializeComponent();
            _Diagrams = new List<DiagramPanel>();
        }

        #region Public Methods

        public void NewProject()
        {
            _TabControl.Controls.Clear();
            _Diagrams.Clear();
        }

        public void OpenDiagram(DiagramPanel diagram)
        {
            if (_Diagrams.Contains(diagram))
            {
                _TabControl.SelectTab(_Diagrams.IndexOf(diagram));
            }
            else
            {
                diagram.MainForm = _MainForm;

                _Diagrams.Add(diagram);
                TabPage tabPage = new TabPage();
                tabPage.Controls.Add(diagram);
                tabPage.Text = diagram.Name;
                diagram.Dock = DockStyle.Fill;

                _TabControl.Controls.Add(tabPage);

                _TabControl.SelectedTab = tabPage;

                _MainForm.FocusDiagram = diagram;
            }
        }

        public void OpenDiagram(Screen screen)
        {
            foreach (DiagramPanel diagram in _Diagrams)
            {
                if (diagram.Screen == screen)
                {
                    OpenDiagram(diagram);
                    break;
                }
            }
        }

        public void CloseDiagram(DiagramPanel diagram)
        {
            if (_Diagrams.Contains(diagram))
            {
                TabPage tabPage = _TabControl.TabPages[_Diagrams.IndexOf(diagram)];
                _TabControl.Controls.Remove(tabPage);
                _Diagrams.Remove(diagram);
            }

            if (_TabControl.SelectedIndex >= 0)
            {
                _MainForm.FocusDiagram = _Diagrams[_TabControl.SelectedIndex];
            }
            else
            {
                _MainForm.FocusDiagram = null;
            }
        } 
        #endregion

        #region Private Methods

        private void _TabControl_Selected(object sender, TabControlEventArgs e)
        {
            if (_TabControl.SelectedIndex >= 0)
            {
                _MainForm.FocusDiagram = _Diagrams[_TabControl.SelectedIndex];
            }
            else
            {
                _MainForm.FocusDiagram = null;
            }
        } 
        #endregion

        public void OpenProject()
        {
            
        }

        public void Show()
        {
            this.Visible = true;

            if (_TabControl.SelectedIndex >= 0)
            {
                _MainForm.FocusDiagram = _Diagrams[_TabControl.SelectedIndex];
            }
            else
            {
                _MainForm.FocusDiagram = null;
            }
        }
    }
}
